package kr.hkit.android_caloriecheck.adapter;

import java.util.ArrayList;

import kr.hkit.android_caloriecheck.R;
import kr.hkit.android_caloriecheck.dao.DAO;
import kr.hkit.android_caloriecheck.dto.DayInfo;
import kr.hkit.android_caloriecheck.dto.MyItem;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CalendarAdapter extends BaseAdapter {
	private ArrayList<DayInfo> dayList;
	private Context mContext;
	private int resource;
	private LayoutInflater inflater;
	DAO dao;
	
	public CalendarAdapter(Context context, int textResource,
			ArrayList<DayInfo> dayList) {
		this.mContext = context;
		this.dayList = dayList;
		this.resource = textResource;
		this.inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		dao = DAO.open(mContext);
	}

	public int getCount() {
		return dayList.size();
	}

	public Object getItem(int position) {
		return dayList.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		DayInfo day = dayList.get(position);

		DayViewHolde dayViewHolder;

		if (convertView == null) {
			convertView = inflater.inflate(resource, null);

			if (position % 7 == 6) {
				convertView.setLayoutParams(new GridView.LayoutParams(
						getCellWidthDP() + getRestCellWidthDP(),
						getCellHeightDP()));
			} else {
				convertView.setLayoutParams(new GridView.LayoutParams(
						getCellWidthDP(), getCellHeightDP()));
			}

			dayViewHolder = new DayViewHolde();
			dayViewHolder.background = (LinearLayout) convertView
					.findViewById(R.id.day_background);
			dayViewHolder.tvDay = (TextView) convertView.findViewById(R.id.day);

			convertView.setTag(dayViewHolder);

		} else {
			dayViewHolder = (DayViewHolde) convertView.getTag();
		}

		if (day != null) {
			dayViewHolder.tvDay.setText(String.valueOf(day.getDay()));

			if (day.isInMonth()) {
				if (position % 7 == 0) {
					dayViewHolder.tvDay.setTextColor(Color.RED);
				} else if (position % 7 == 6) {
					dayViewHolder.tvDay.setTextColor(Color.BLUE);
				} else {
					dayViewHolder.tvDay.setTextColor(Color.BLACK);
				}
			} else {
				dayViewHolder.tvDay.setTextColor(Color.GRAY);
			}
		}
		
		ArrayList<MyItem> arr= dao.foodListSelectByDate(day.getYear(), day.getMonth(), day.getDay());
		ArrayList<MyItem> arr2 = dao.exerciseListSelectByDate(day.getYear(), day.getMonth(), day.getDay());
		if(arr.size() != 0 || arr2.size() != 0){
			dayViewHolder.tvDay.setBackgroundColor(Color.rgb(204, 255, 153));
			dayViewHolder.tvDay.setPaintFlags(Paint.FAKE_BOLD_TEXT_FLAG);
		}
				
		return convertView;
	}

	public class DayViewHolde {
		public LinearLayout background;
		public TextView tvDay;
	}

	private int getCellWidthDP() {
		int width = mContext.getResources().getDisplayMetrics().widthPixels;
		int cellWidth = width / 7;

		return cellWidth;
	}

	private int getRestCellWidthDP() {
		int width = mContext.getResources().getDisplayMetrics().widthPixels;
		int cellWidth = width % 7;

		return cellWidth;
	}

	private int getCellHeightDP() {
		int height = mContext.getResources().getDisplayMetrics().widthPixels;
		int cellHeight = height / 6;

		return cellHeight;
	}
}
